<!-- resister -->
<template>
  <div class="signup-page mx-auto p-3 w-330">
    <h5 class="my-4 text-center">注册者也账户</h5>
    <validate-form @form-submit="onFormSubmit">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          :rules="emailRules" v-model="formData.email"
          placeholder="请输入邮箱地址"
          type="text"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">昵称</label>
        <validate-input
          :rules="nameRules" v-model="formData.nickName"
          placeholder="请输入昵称"
          type="text"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          type="password"
          placeholder="请输入密码"
          :rules="pwdRules"
          v-model="formData.password"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">重复密码</label>
        <validate-input
          type="password"
          placeholder="请再次密码"
          :rules="repeatPwdRules"
          v-model="formData.repeatPassword"
        />
      </div>
      <template #submit>
        <button type="submit" class="btn btn-primary btn-block btn-large">注册新用户</button>
      </template>
    </validate-form>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive } from "vue";
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import ValidateForm from "@/components/ValidateForm.vue";
import ValidateInput, { RulesProp } from "@/components/ValidateInput.vue";
import createMessage from '@/components/CreateMessage'
export default defineComponent({
  name: "resiger",
  props: {},
  components: {
    ValidateInput,
    ValidateForm,
  },

  setup(props) {
    
    const formData = reactive({
      email: '1@163.com',
      nickName: '1',
      password: '1',
      repeatPassword: '1'
    });

    const emailRules: RulesProp = [
      { type: "required", message: "电子邮箱地址不能为空" },
      { type: "email", message: "请输入正确的电子邮箱格式" },
    ];

    const nameRules: RulesProp = [
       { type: 'required', message: '昵称不能为空' }
    ];

    const pwdRules: RulesProp = [
      { type: "required", message: "密码不能为空" },
      { type: "password", message: "请输入正确的密码" },
    ];

    const repeatPwdRules: RulesProp = [
        { type: 'required', message: '重复密码不能为空' },
        {
          type: 'custom',
          validator: () => {
            return formData.password === formData.repeatPassword
          },
          message: '两次输入密码不相同'
        }
    ]

    const router = useRouter();
    const store = useStore();
    const onFormSubmit = (res: boolean) => {
      console.log("register--",res)
        if(res) {
          const payload = {
            email: formData.email,
            password: formData.password,
            nickName: formData.nickName
          }
          store.commit('setLoading', true)
          setTimeout(() => {
             store.commit('setLoading', false)
             createMessage('注册成功 正在跳转登录页面', 'success')
          }, 2000)

           setTimeout(() => {
             router.push('/login')
          }, 3000)

        }  
    };

    return {
      formData,
      emailRules,
      nameRules,
      pwdRules,
      repeatPwdRules,
      onFormSubmit,
    };
  },
});
</script>
<style scoped>
.w-330 {
  max-width: 550px;
}
</style>